package com.sky.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类管理")
@RequiredArgsConstructor
public class CategoryController {
    private final CategoryService categoryService;

    @GetMapping("page")
    @ApiOperation("分页查询")
    public Result<PageResult> selectPage(CategoryPageQueryDTO dto) {
        PageResult result = categoryService.selectCategoryPage(dto);
        return Result.success(result);
    }

    @PutMapping
    @ApiOperation("编辑分类")
    public Result update(@RequestBody CategoryDTO dto){
        Category category = BeanUtil.toBean(dto, Category.class);
        categoryService.updateById(category);
        return Result.success();
    }
    @PostMapping("status/{status}")
    @ApiOperation("修改分类状态")
    public Result updateStatus(@PathVariable Integer status,Long id){
        categoryService.update(Wrappers.<Category>lambdaUpdate().eq(Category::getId,id).set(Category::getStatus,status));
        return Result.success();
    }

    @PostMapping
    @ApiOperation("新增分类")
    public Result insert(@RequestBody CategoryDTO dto){
        Category category = BeanUtil.toBean(dto, Category.class);
        category.setStatus(1);
        categoryService.save(category);
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result delete(Long id){
        categoryService.removeCategoryById(id);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> selectByType(Integer type){
        List<Category> list = categoryService.list(Wrappers.<Category>lambdaQuery().eq(Category::getType, type));
        return Result.success(list);
    }


}
